{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "达到递归限制，您的graph将返回一个错误，这对于所有用例来说可能并不理想。相反，您可能希望在达到递归限制之前返回状态的值。本操作指南将展示如何做到这一点。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "49765b794ee3d408"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "\n",
    "from typing_extensions import TypedDict\n",
    "from langgraph.graph import StateGraph\n",
    "from langgraph.constants import END, START\n",
    "\n",
    "\n",
    "class State(TypedDict):\n",
    "    value: str\n",
    "    action_result: str\n",
    "\n",
    "\n",
    "def router(state: State):\n",
    "    print(state)\n",
    "    if state[\"value\"] == \"end\":\n",
    "        return END\n",
    "    else:\n",
    "        return \"action\"\n",
    "\n",
    "\n",
    "# 决策节点\n",
    "def decision_node(state):\n",
    "    return {\"value\": \"keep going!\"}\n",
    "\n",
    "\n",
    "def action_node(state: State):\n",
    "    return {\"action_result\": \"what a great result!\"}\n",
    "\n",
    "\n",
    "workflow = StateGraph(State)\n",
    "#添加节点\n",
    "workflow.add_node(\"decision\", decision_node)\n",
    "workflow.add_node(\"action\", action_node)\n",
    "#设置开始节点\n",
    "workflow.add_edge(START, \"decision\")\n",
    "#设置边条件\n",
    "workflow.add_conditional_edges(\"decision\", router, [\"action\", END])\n",
    "workflow.add_edge(\"action\", \"decision\")\n",
    "\n",
    "graph = workflow.compile()\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-04T06:21:43.719767Z",
     "start_time": "2024-11-04T06:21:43.710514Z"
    }
   },
   "id": "176c202446158f5f",
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/4gHYSUNDX1BST0ZJTEUAAQEAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQA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iIudBERAREQEREBERAREQEREBERAREQf/2Q==",
      "text/plain": "<IPython.core.display.Image object>"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.core.display import Image\n",
    "\n",
    "display(Image(graph.get_graph().draw_mermaid_png()))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-04T06:21:44.462380Z",
     "start_time": "2024-11-04T06:21:43.723521Z"
    }
   },
   "id": "2b3fb2359cdb5990",
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'value': 'keep going!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!'}\n",
      "Recursion Error\n"
     ]
    }
   ],
   "source": [
    "from langgraph.errors import GraphRecursionError\n",
    "\n",
    "try:\n",
    "    graph.invoke({\"value\": \"hi!\"})\n",
    "except GraphRecursionError:\n",
    "    print(\"Recursion Error\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-04T06:21:44.491424Z",
     "start_time": "2024-11-04T06:21:44.468430Z"
    }
   },
   "id": "b8c02005f0f7f896",
   "execution_count": 18
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 返回状态\n",
    "Recursion Error时返回状态，上面代码微调,在State中增加RemainingSteps(依赖:`langgraph.managed.is_last_step`)\n",
    "\n",
    "Since our action node is going to always induce at least 2 extra steps to our graph (since the action node ALWAYS calls the decision node afterwards), we will use this channel to check if we are within 2 steps of the limit."
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "168f6f2e9f6afdfa"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "\n",
    "from langgraph.managed.is_last_step import RemainingSteps\n",
    "from typing_extensions import TypedDict\n",
    "from langgraph.graph import StateGraph\n",
    "from langgraph.constants import END, START\n",
    "\n",
    "\n",
    "class State(TypedDict):\n",
    "    value: str\n",
    "    action_result: str\n",
    "    # 剩余步骤\n",
    "    remaining_steps: RemainingSteps\n",
    "\n",
    "\n",
    "def router(state: State):\n",
    "    print(state)\n",
    "    print(f\"Remaining Steps: {state['remaining_steps']}\")\n",
    "    if state[\"remaining_steps\"] <= 2:\n",
    "        return END\n",
    "    \n",
    "    if state[\"value\"] == \"end\":\n",
    "        return END\n",
    "    else:\n",
    "        return \"action\"\n",
    "\n",
    "\n",
    "# 决策节点\n",
    "def decision_node(state):\n",
    "    return {\"value\": \"keep going!\"}\n",
    "\n",
    "\n",
    "def action_node(state: State):\n",
    "    return {\"action_result\": \"what a great result!\"}\n",
    "\n",
    "\n",
    "workflow = StateGraph(State)\n",
    "#添加节点\n",
    "workflow.add_node(\"decision\", decision_node)\n",
    "workflow.add_node(\"action\", action_node)\n",
    "#设置开始节点\n",
    "workflow.add_edge(START, \"decision\")\n",
    "#设置边条件\n",
    "workflow.add_conditional_edges(\"decision\", router, [\"action\", END])\n",
    "workflow.add_edge(\"action\", \"decision\")\n",
    "\n",
    "graph = workflow.compile()\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-04T06:30:46.358494Z",
     "start_time": "2024-11-04T06:30:46.349021Z"
    }
   },
   "id": "c6c803e86a920099",
   "execution_count": 31
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'value': 'keep going!', 'remaining_steps': 24}\n",
      "Remaining Steps: 24\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!', 'remaining_steps': 22}\n",
      "Remaining Steps: 22\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!', 'remaining_steps': 20}\n",
      "Remaining Steps: 20\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!', 'remaining_steps': 18}\n",
      "Remaining Steps: 18\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!', 'remaining_steps': 16}\n",
      "Remaining Steps: 16\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!', 'remaining_steps': 14}\n",
      "Remaining Steps: 14\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!', 'remaining_steps': 12}\n",
      "Remaining Steps: 12\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!', 'remaining_steps': 10}\n",
      "Remaining Steps: 10\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!', 'remaining_steps': 8}\n",
      "Remaining Steps: 8\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!', 'remaining_steps': 6}\n",
      "Remaining Steps: 6\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!', 'remaining_steps': 4}\n",
      "Remaining Steps: 4\n",
      "{'value': 'keep going!', 'action_result': 'what a great result!', 'remaining_steps': 2}\n",
      "Remaining Steps: 2\n"
     ]
    },
    {
     "data": {
      "text/plain": "{'value': 'keep going!', 'action_result': 'what a great result!'}"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph.invoke({\"value\": \"hi!\"})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-04T06:30:48.801610Z",
     "start_time": "2024-11-04T06:30:48.781606Z"
    }
   },
   "id": "d11b3278d61604de",
   "execution_count": 32
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
